﻿/**
 * 调查管理
 * 
 * @author liuxl
 * @createTime 2011-03-28
 */
Ext.ux.SurveryPanel = function() {
	var desktop = MyDesktop.getDesktop();
	var surveryWins = desktop.getWindow('survery-view-win');
	var cId = 0;
	var cName = null;

	if (!surveryWins) {

		var index = 0;
		/**
		 * 栏目 增、删、改
		 */
		// 开始创建Add面板
		var addCForm = new Ext.FormPanel({
			layout : 'form',
			border : false,
			labelSeparator : '：',
			frame : true,
			labelWidth : 60,
			labelAlign : 'left',
			buttonAlign : 'right',
			bodyStyle : 'padding:5px',
			width : 220,
			fileUpload : true, // 允许上传文件
			enctype : 'multipart/form-data',
			items : [{
						xtype : 'textfield',
						fieldLabel : '名称',
						name : 'name',
						maxLength : 100,
						maxLengthText : '不能超过100个字符',
						allowBlank : false,
						blankText : '名称不能为空！',
						anchor : '96%'
					}],
			buttons : [{
				id : 'btnSave',
				text : '保 存',
				handler : function() {
					var categoryParentId = cId;
					if (addCForm.form.isValid())// 客户端先验证数据
					{
						addCForm.form.doAction('submit', {
									url : '/admin/qCategory.jhtml?method=addQuestionCategory',
									method : 'post',
									waitTitle : '请稍候',
									waitMsg : '正在提交表单数据，请稍候...',
									success : function(form, action) {
										getAlertMessage("提示","操作成功!",Ext.MessageBox.WARNING);
										addCForm.form.reset();
										this.disabled = false;
										newC1win.hide();
										Ext.getCmp("surTrees").root.reload();
									},
									failure : function() {
										getAlertMessage("提示","保存失败!",Ext.MessageBox.WARNING);
										this.disabled = false;
									}
								})
					}
				}

			}, {
				text : '取 消',
				handler : function() {
					newC1win.hide();
				}
			}]
		});
		function showAddCPanel() {
			newC1win = new Ext.Window({
						xtype : 'window',
						title : '添加类别',
						buttonAlign : 'right',
						closable : true,
						layout : 'fit',
						modal : 'true',
						width : 240,
						height : 130,
						closeAction : 'hide',
						plain : true,
						items : addCForm

					});
			newC1win.show();
		};

		var editCForm = new Ext.FormPanel({
			layout : 'form',
			border : false,
			labelSeparator : '：',
			frame : true,
			labelWidth : 60,
			labelAlign : 'left',
			buttonAlign : 'right',
			bodyStyle : 'padding:5px',
			fileUpload : true, // 允许上传文件
			enctype : 'multipart/form-data',
			width : 220,
			reader : new Ext.data.JsonReader({
						root : 'results'
					}, ['id', 'name','corder']),
			items : [{
						xtype : 'hidden',
						name : 'id',
						anchor : '96%'

					}, {
						xtype : 'textfield',
						fieldLabel : '名称',
						name : 'name',
						allowBlank : false,
						blankText : '名称不能为空！',
						maxLength : 100,
						maxLengthText : '名称不能超过100个字符',
						anchor : '96%'
					},{
						xtype : 'textfield',
						fieldLabel : '排序',
						name : 'corder',
						anchor : '96%'
					}],
			buttons : [{
				id : 'btnSave',
				text : '保 存',
				handler : function() {
					if (editCForm.form.isValid())// 客户端先验证数据
					{

						editCForm.form.doAction('submit', {
									url : '/admin/qCategory.jhtml?method=updateQuestionCategory',
									method : 'post',
									waitTitle : '请稍候',
									waitMsg : '正在提交表单数据，请稍候...',
									success : function(form, action) {
										getAlertMessage("提示","数据修改成功!",Ext.MessageBox.WARNING);
										this.disabled = false;
										this.newwinC.hide();
										Ext.getCmp("surTrees").root.reload();
									},
									failure : formError
								})
					}
				}

			}, {
				text : '取 消',
				handler : function() {
					newwinC.hide();
				}
			}]
		});
		// 编辑信息
		function showEditCPanel() {
			newwinC = new Ext.Window({
						xtype : 'window',
						title : '修改类别',
						buttonAlign : 'right',
						closable : true,
						layout : 'fit',
						modal : 'true',
						width : 240,
						height : 150,
						closeAction : 'hide',
						plain : true,
						items : editCForm

					});
			var urlPath = "/admin/qCategory.jhtml?method=getQuestionCategory";
			editCForm.form.load({
						url : urlPath,
						params : {
							questionCategoryId : cId
						},
						method : 'post',
						success : function(form, action) {
						},
						failure : formError

					});
			newwinC.show();
		};
		/**
		 * 栏目 end
		 */
		surveryWins = desktop.createWindow({
			id : 'survery-view-win',
			title : '投票管理',
			closable : true,
			modal : 'true',
			width : 900,
			height : 550,
			layout : "border",
			items : [{
				id : 'spider-panel',
				region : 'west',
				split : true,
				width : 170,
				minSize : 175,
				maxSize : 400,
				collapsible : true,
				margins : '0 0 5 5',
				cmargins : '0 0 0 0',
				layout : 'fit',
				bodyStyle : 'background-color:#DFE8F6',
				layoutConfig : {
					animate : true,
					sequence : true
				},

				items : [{
					xtype : 'treepanel',
					id : 'surTrees',
					border : false,
					rootVisible : true,
					autoScroll : true,
					loader : new Ext.tree.TreeLoader({
						dataUrl : '/admin/qCategory.jhtml?method=getQuestionTree'
					}),
					root : new Ext.tree.AsyncTreeNode({
								id : "root",
								text : "类别导航"
							}),
					listeners : {
						'click' : function(n) {
							try {
								var sn = this.selModel.selNode || {};
								if (n.leaf && n.id != sn.id) {
									var categoryId = n.id;
									cId = n.id;
									var ctypeId = null;

									var tab = Ext.getCmp('survery-panel')
											.getComponent(categoryId);
									if (tab) {
										Ext.getCmp('survery-panel')
												.setActiveTab(tab);
									} else {
									
		var _jsonReader1 = new Ext.data.JsonReader({
					root : 'results',
					totalProperty : 'totalCount'
				}, [{
							name : 'id',
							mapping : 'id'
						},{
							name : 'content',
							mapping : 'content'
						},{
							name : 'date',
							mapping : 'date'
						},{
							name : 'endDateStr',
							mapping : 'endDateStr'
						}]);
		var ds_survey = new Ext.data.Store({
					proxy : new Ext.data.HttpProxy({
								url : '/admin/question.jhtml?method=getQuestions&questionCategoryId='+categoryId
							}),
					reader : _jsonReader1
				});
		var cm_keyword = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
				new Ext.grid.CheckboxSelectionModel(), {
					header : "编号",
					width : 80,
					sortable : true,
					dataIndex : 'id'
				},{
					header : "标题",
					width : 200,
					sortable : true,
					dataIndex : 'content'
				},  {
					header : "结束时间",
					width : 100,
					sortable : true,
					dataIndex : 'endDateStr'
				},  {
					header : "创建时间",
					width : 100,
					sortable : true,
					dataIndex : 'date'
				}]);
		var grid_survey = new Ext.grid.GridPanel({
			region : "center",
			cm : cm_keyword,
			sm : new Ext.grid.CheckboxSelectionModel(),
			border : false,
			loadMask : {
				msg : "数据加载中，请稍等..."
			},
			store : ds_survey,
			viewConfig : {
				forceFit : true
			},
			bbar : new Ext.PagingToolbar({
						pageSize : 20,
						store : ds_survey,
						displayInfo : true
					}),
			tbar : [{
                    text: '添加',
                    tooltip: '添加调查',
                    iconCls: 'add',
                    handler: function(){
				// 开始创建Add面板
				var addCForm = new Ext.FormPanel( {
					layout :'form',
					border :false,
					labelSeparator :'：',
					frame :true,
					labelWidth :60,
					labelAlign :'left',
					buttonAlign :'right',
					bodyStyle :'padding:5px',
					width :330,
					fileUpload :true, // 允许上传文件
					enctype :'multipart/form-data',
					items : [ {
						xtype :'textfield',
						fieldLabel :'标题',
						name :'content',
						id:'content',
						maxLength :500,
						maxLengthText :'不能超过500个字符',
						allowBlank :false,
						blankText :'名称不能为空！',
						anchor :'96%'
					} ,{
						xtype :'textfield',
						fieldLabel :'结束日期',
						name :'endDateStr',
						id:'endDateStr',
						maxLength :500,
						maxLengthText :'不能超过500个字符',
						anchor :'96%'
					},{
						xtype :'textarea',
						fieldLabel :'描述',
						name :'questionDesc',
						id:'questionDesc',
						maxLength :1024,
						maxLengthText :'不能超过1024个字符',
						anchor :'96%'
					}],
					buttons : [ {
						id :'btnSave',
						text :'保 存',
						handler : function() {
						
						if (addCForm.form.isValid())// 客户端先验证数据
						{
							//var kkk = encodeURIComponent(addCForm.findById('keyword').getValue());

							// this.disabled = true; //禁止保存按钮，防止多次提交
						addCForm.form.doAction('submit', {
							url :'/admin/question.jhtml?method=addQuestion',
						//	method :'get',
							params:{questionCategoryId:categoryId},
							waitTitle :'请稍候',
							waitMsg :'正在提交表单数据，请稍候...',
							success : function(form, action) {
								getAlertMessage("提示","操作成功!",Ext.MessageBox.WARNING);
								addCForm.form.reset();
								this.disabled = false;
								newC1win.close();
								
								ds_survey.load( {
									params : {
										start :0,
										limit :20
									}
								});// 重新加载grid的数据
							},
							failure : function() {
								getAlertMessage("提示","保存失败!",Ext.MessageBox.WARNING);
								this.disabled = false;
							}
						})
					}
				}

					}, {
						text :'取 消',
						handler : function() {
						newC1win.close();
					}
					} ]
				});
			
					newC1win = new Ext.Window( {
						xtype :'window',
						title :'添加调查',
						buttonAlign :'right',
						closable :true,
						layout :'fit',
						modal :'true',
						width :350,
						height :250,
						closeAction :'close',
						plain :true,
						items :addCForm

					});
					newC1win.show();

			}
                },'-',{
                    text: '修改',
                    tooltip: '修改调查',
               		 cls : 'x-btn-text-icon',
					icon : '/static/images/desktop/icon/165518105.gif',
                    handler: function(){
				// 开始创建Add面板
				var deitCForm = new Ext.FormPanel( {
					layout :'form',
					border :false,
					labelSeparator :'：',
					frame :true,
					labelWidth :60,
					labelAlign :'left',
					buttonAlign :'right',
					bodyStyle :'padding:5px',
					width :330,
					fileUpload :true, // 允许上传文件
					enctype :'multipart/form-data',
					reader : new Ext.data.JsonReader({
								root : 'results'
							}, ['id', 'content', 'endDateStr',
									'questionDesc']),
					items : [ {
								xtype : 'hidden',
								name : 'id',
								anchor : '96%'
							},{
						xtype :'textfield',
						fieldLabel :'标题',
						name :'content',
						id:'content',
						maxLength :500,
						maxLengthText :'不能超过500个字符',
						allowBlank :false,
						blankText :'名称不能为空！',
						anchor :'96%'
					} ,{
						xtype :'textfield',
						fieldLabel :'结束日期',
						name :'endDateStr',
						id:'endDateStr',
						maxLength :500,
						maxLengthText :'不能超过500个字符',
						anchor :'96%'
					},{
						xtype :'textarea',
						fieldLabel :'描述',
						name :'questionDesc',
						id:'questionDesc',
						maxLength :1024,
						maxLengthText :'不能超过1024个字符',
						anchor :'96%'
					}],
					buttons : [ {
						id :'btnSave',
						text :'保 存',
						handler : function() {
						
						if (deitCForm.form.isValid())// 客户端先验证数据
						{
						deitCForm.form.doAction('submit', {
							url :'/admin/question.jhtml?method=updateQuestion',
							waitTitle :'请稍候',
							waitMsg :'正在提交表单数据，请稍候...',
							success : function(form, action) {
								getAlertMessage("提示","操作成功!",Ext.MessageBox.WARNING);
								deitCForm.form.reset();
								this.disabled = false;
								newC1win.close();
								
								ds_survey.load( {
									params : {
										start :0,
										limit :20
									}
								});// 重新加载grid的数据
							},
							failure : function() {
								getAlertMessage("提示","保存失败!",Ext.MessageBox.WARNING);
								this.disabled = false;
							}
						})
					}
				}

					}, {
						text :'取 消',
						handler : function() {
						newC1win.close();
					}
					} ]
				});
			
					newC1win = new Ext.Window( {
						xtype :'window',
						title :'修改调查',
						buttonAlign :'right',
						closable :true,
						layout :'fit',
						modal :'true',
						width :350,
						height :250,
						closeAction :'close',
						plain :true,
						items :deitCForm

					});
					var row = grid_survey.getSelections();
				if (row == 0) {
					getAlertMessage("提示","请确认是否选中要编辑的信息!",Ext.MessageBox.WARNING);
					return;
				}
				var id = row[0].get("id");
				var urlPath = "/admin/question.jhtml?method=getQuestion";
				deitCForm.form.load({

							url : urlPath,
							params : {
								questionId : id
							},
							method : 'post',
							success : function(form, action) {
							},
							failure : function(form, action) {
								getAlertMessage("提示","数据加载失败!",Ext.MessageBox.WARNING);
							}

						});
					newC1win.show();

			}
                },'-',{
                
			text : '删除',
			tooltip : '挂起选中的一个',
			iconCls : 'remove',
			handler : function() {

				var rows = grid_survey.getSelectionModel().getSelections();// 返回值为
				// Record
				// 数组
				if (rows.length == 0) {
					getAlertMessage("警告","最少选择一条信息，进行删除!",Ext.MessageBox.WARNING);
				} else {
					Ext.MessageBox.confirm('提示框', '您确定要进行该操作？', function(btn) {
						if (btn == 'yes') {

							if (rows) {
								for (var i = 0; i < rows.length; i++) {
									var conn = new Ext.data.Connection();
									conn.request({
										url : '/admin/question.jhtml?method=deleteQuestion',
										params : {
											questionId : rows[i]
													.get("id")
										}

									});

									break;
								}

							}
							loadDs(ds_survey,20);
						}
					});
				} // 弹出对话框警告
			}
		
                },'-',{
                
															text : '查看选项',
															tooltip : '查看选中的一列',
															cls : 'x-btn-text-icon',
															icon : '/static/images/desktop/icon/16551842.gif',
															handler : function() {
																var rows = grid_survey
																		.getSelectionModel()
																		.getSelections();// 返回值为
																if (rows.length == 0) {
																	getAlertMessage("提示","最少选择一条信息，进行操作!",Ext.MessageBox.WARNING);
																} else {
																	publicCId = rows[0].get("id");
																	commentType = 1;
																	loadClass("Ext.ux.OptionPanel","option-win");
																} // 弹出对话框警告
															}
														
                },'-',{
                
                
		

			text : '生成链接',
			tooltip : '生成链接',
			cls : 'x-btn-text-icon',
			icon : 'static/images/desktop/icons/arrow-up.gif',
			handler : function() {

				var rows = grid_survey.getSelectionModel().getSelections();// 返回值为
				// Record
				// 数组
				if (rows.length == 0) {
					getAlertMessage("提示","选择一条信息，进行操作!",Ext.MessageBox.WARNING);
				} else {

					var editForm = new Ext.FormPanel({
						layout : 'form',
						border : false,
						labelSeparator : '：',
						frame : true,
						labelWidth : 60,
						labelAlign : 'left',
						buttonAlign : 'right',
						bodyStyle : 'padding:5px',
						width : 330,
						reader : new Ext.data.JsonReader({
									root : 'results'
								}, ['content', 'endDateStr','questionDesc']),
						items : [{
									xtype : 'textfield',
									fieldLabel : '标题',
									name : 'content',
									anchor : '96%'
								}, {
								xtype : 'textarea',
								fieldLabel : '摘要',
								name : 'questionDesc',
								anchor : '96%'
							}, {
									xtype : 'textfield',
									fieldLabel : '链接',
									name : 'endDateStr',
									anchor : '96%'
								}],
						buttons : [{
							text : '关 闭',
							handler : function() {
								editwin.close();
							}
						}]
					});
					editwin = new Ext.Window({
								xtype : 'window',
								title : '链接生成',
								buttonAlign : 'right',
								closable : true,
								layout : 'fit',
								modal : 'true',
								width : 350,
								height : 260,
								closeAction : 'close',
								plain : true,
								items : editForm
							});
					var rows = grid_survey.getSelectionModel().getSelections();
					if (rows == 0) {
						getAlertMessage("提示","请确认是否选中要作者的信息!",Ext.MessageBox.WARNING);
						return;
					}
					var id = rows[0].get("id");
					editForm.form.load({
								url : 'admin/question.jhtml?method=createUrl',
								params : {
									questionId : id
								},
								method : 'post',
								success : function(form, action) {
								}
							});
					editwin.show();

				} // 弹出对话框警告

			}

		
		
                
                }]
		});
		Ext.getCmp('survery-panel').add({
													title : n.text + ' 类别 ',
													id : categoryId,
													iconCls : 'tabs',
													layout : 'fit',
													items : grid_survey,
													closable : true
												}).show();

										grid_survey.store.load({
													params : {
														start : 0,
														limit : 20,
														questionCategoryId : categoryId
													}
												});
				
									}

								}
							} catch (e) {
							}
						},
						'contextmenu' : function(node, e) {
						if(node.id=='root'){
						
							cId = node.id;
							function onEdit(node) {

								showEditCPanel();

							};
							function onADD() {
								showAddCPanel();
							};
							function onDelte() {
								var conn = new Ext.data.Connection();
								conn.request({
									url : '/admin/qCategory.jhtml?method=deleteQuestionCategory',
									params : {
										questionCategoryId : cId
									},
									success : function(form, action) {
										getAlertMessage("提示","删除成功!",Ext.MessageBox.WARNING);
										Ext.getCmp("surTrees").root.reload();
									},
									failure : function() {
										getAlertMessage("提示","保存失败!",Ext.MessageBox.WARNING);
									}
								});
							};
							new Ext.menu.Menu({
										items : [{
													text : '添加',
													iconCls : 'menut',
													handler : onADD
												}, {
													text : '修改',
													iconCls : 'menut',
													handler : onEdit
												}, {
													text : '删除',
													iconCls : 'menut',
													handler : onDelte
												}]
									}).showAt(e.getPoint());
							categoryId = node.id;

						
						
						}else{
						
						
							cId = node.id;
							function onEdit(node) {

								showEditCPanel();

							};
							function onDelte() {
								var conn = new Ext.data.Connection();
								conn.request({
									url : '/admin/qCategory.jhtml?method=deleteQuestionCategory',
									params : {
										questionCategoryId : cId
									},
									success : function(form, action) {
										getAlertMessage("提示","删除成功!",Ext.MessageBox.WARNING);
										Ext.getCmp("surTrees").root.reload();
									},
									failure : function() {
										getAlertMessage("提示","保存失败!",Ext.MessageBox.WARNING);
									}
								});
							};
							new Ext.menu.Menu({
										items : [ {
													text : '修改',
													iconCls : 'menut',
													handler : onEdit
												}, {
													text : '删除',
													iconCls : 'menut',
													handler : onDelte
												}]
									}).showAt(e.getPoint());
							categoryId = node.id;

						
						
						
						}
						}
					}

				}]
			}, {
				xtype : 'tabpanel',
				id : 'survery-panel',
				region : 'center',
				activeItem : 0,
				resizeTabs : true, // turn on tab resizing
				enableTabScroll : true,
				border : false,
				plugins : new Ext.ux.TabCloseMenu(),
				items : [getStartPanel('surveryStart')]
			}]
		});

	}
	surveryWins.show();
}
